package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 */
@Service
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    //新增会员
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    //根据月份查询注册会员总数量
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> list = new ArrayList<>();
        if(months != null && months.size() > 0){
            for (String month : months) {//2019.01
                month += ".31";//2019.01.31
                Integer count = memberDao.findMemberCountBeforeDate(month);
                list.add(count);
            }
        }
        return list;
    }

    //根据性别查询会员数量
    public List<Map<String, Object>> findMemberCountBySex() {
        return memberDao.findMemberCountBySex();
    }

    //根据年龄查询会员数量
    public List<Map<String, Object>> findMemberCountByAge() {
        return memberDao.findMemberCountByAge();
    }
}
